package com.asktgapp.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.application.MyApplication;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.engineer.activity.EditWithDrawPwdActivity;
import com.asktgapp.engineer.activity.IDVerificationActivity;
import com.asktgapp.hxsg.BiuEaseHelper;
import com.asktgapp.user.activity.LoginActivity;
import com.asktgapp.user.activity.OpinionActivity;
import com.asktgapp.user.activity.RevisePwdActivity;
import com.asktgapp.user.activity.UpdateActivty;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.umeng.message.IUmengCallback;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * time:2018/1/8
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class SettingFragment extends BaseFragment {
    /**
     * 找回登录密码
     */
    @InjectView(R.id.tv_setting_find_pwd)
    TextView findPwdTv;

    /**
     * 修改提现密码
     */
    @InjectView(R.id.tv_setting_find_withdraw)
    TextView findWithDrawTv;

    /**
     * 检查更新
     */
    @InjectView(R.id.tv_setting_update)
    TextView updateTv;
    /**
     * 消息提醒
     */
    @InjectView(R.id.chk_setting_message_remind)
    CheckBox messageRemindChk;

    @InjectView(R.id.tv_setting_logout)
    TextView logOut;

    @InjectView(R.id.tvFanK)
    TextView tvFanK;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_setting, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        findPwdTv.setOnClickListener(this);
        findWithDrawTv.setOnClickListener(this);
        updateTv.setOnClickListener(this);
        logOut.setOnClickListener(this);
        tvFanK.setOnClickListener(this);
        if (PreferencesUtil.getBoolean2(getActivity(), PreferencesUtil.KEY_PUSH)) {
            messageRemindChk.setChecked(true);
        } else {
            messageRemindChk.setChecked(false);
        }
        // TODO: 2018/1/18 根据身份来释放修改提现密码的隐藏状态

        messageRemindChk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    // TODO: 2018/1/8 开通消息提醒功能
                    MyApplication.mPushAgent.enable(new IUmengCallback() {

                        @Override
                        public void onSuccess() {
                            PreferencesUtil.putBoolean(getActivity(), PreferencesUtil.KEY_PUSH, true);
                        }

                        @Override
                        public void onFailure(String s, String s1) {

                        }

                    });
                } else {
                    // TODO: 2018/1/8 关闭消息提醒功能
                    MyApplication.mPushAgent.disable(new IUmengCallback() {

                        @Override
                        public void onSuccess() {
                            PreferencesUtil.putBoolean(getActivity(), PreferencesUtil.KEY_PUSH, false);
                        }

                        @Override
                        public void onFailure(String s, String s1) {

                        }

                    });
                }

            }
        });
        if (PreferencesUtil.getBoolean(getActivity(), PreferencesUtil.KEY_IS_ENGINEER)) {
            findWithDrawTv.setVisibility(View.VISIBLE);
        } else {
            findWithDrawTv.setVisibility(View.GONE);
        }
    }

    @Override
    public void loadData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_setting_find_pwd:
                // 跳转到修改密码
                startActivity(new Intent(getActivity(), RevisePwdActivity.class));
                break;

            case R.id.tv_setting_find_withdraw:
                if (!PreferencesUtil.getBoolean(getActivity(), PreferencesUtil.KEY_HAS_TX_PWD)) {
                    startActivity(new Intent(getActivity(), IDVerificationActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), EditWithDrawPwdActivity.class));
                }
                //跳转到找回提现密码
                break;
            case R.id.tv_setting_update:
                // 跳转到检查更新
                startActivity(new Intent(getActivity(), UpdateActivty.class));
                break;
            case R.id.tv_setting_logout:
                logout();
                break;
            case R.id.tvFanK:
                startActivity(new Intent(getActivity(), OpinionActivity.class));
                break;
        }
    }

    private void logout() {
        // TODO: 2018/1/8 调用退出接口，在接口成功的回调中，退出环信，清除SharedPreferences中的数据
        PreferencesUtil.putString(getActivity(), PreferencesUtil.USER_ID, "");
        PreferencesUtil.putString(getActivity(), PreferencesUtil.KEY_TOKEN, "");
        PreferencesUtil.putBoolean(getActivity(), PreferencesUtil.KEY_IS_ENGINEER, false);
        BiuEaseHelper.loginOut();
        PreferencesUtil.putString(getActivity(), PreferencesUtil.KEY_HX_ID, "");
        PreferencesUtil.putBoolean(getActivity(), PreferencesUtil.KEY_UMENG_TOKEN, false);
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        getActivity().finish();
    }
}
